home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Compression / tarZan / Source / tarZanObject.m < prev    next >
Text File  |  1992-08-19  |  15KB  |  454 lines

  1.  
  2. /* Generated by Interface Builder */
  3. // Created By James T. Romano July 22, 1992 
  4. // Member of the Dynamic Duo of NeXT Computers!
  5. //
  6. // 
  7. // Read it and Weep!
  8. #import "tarZanObject.h"
  9.  
  10.     BOOL selectionMade = NO;
  11.     char *copyRightNotice = "tarZan was Created By James T. Romano & Salvatore Saieva (C) 1992";
  12.     char *dduoNotice = "The Dynamic Duo of NeXT Computers!";
  13.     char templateName[] = "/tmp/.tarZantarzee",*tempFile,actionMsg[1024],globalName[1024];
  14.     int fd;
  15.     NXStream *tempF = NULL;
  16.     
  17.  
  18.  
  19. @implementation tarZanObject
  20.  
  21. #define SHOWERROR(msg,where) NXRunAlertPanel(msg,where,"OK",NULL,NULL);
  22. - selectMade
  23. {
  24.     selectionMade = YES;
  25.     return self;
  26. }
  27.  
  28. - init 
  29. {
  30.   [self initUsingFile:NULL withType:0];
  31.   return self;
  32. }
  33.  
  34. - initUsingFile:(const char *)fname withType:(int)type
  35. {
  36.   NXRect myFrame;
  37.   unsigned int windowNum;
  38.   char title[80],streamText[1024],tempChar;
  39.   int tempCount = 0;
  40.   
  41.   [super init];
  42.   [NXApp loadNibSection:"tarZanObj.nib" owner:self withNames:NO fromZone:[self zone]];
  43.     
  44.     if (fname) strcpy(globalName,fname);
  45.     /* ********* */
  46.       //BROWSER STUFF
  47.         [browser setDelegate:self];
  48.         [browser setAction:@selector(selectMade)];
  49.         [browser setTitled:NO];
  50.         [browser allowMultiSel:YES];
  51.         [browser hideLeftAndRightScrollButtons:YES];
  52.         [browser setMaxVisibleColumns:1];
  53.         [browser sizeToFit];
  54.       //END
  55.       
  56.       //LISTENER SPEAKER STUFF FOR ICON DRAGGING
  57.         iconPathList = NULL;
  58.         listen = [Listener allocFromZone:[self zone]];
  59.         [listen init];
  60.         [listen setDelegate:self];
  61.         [listen usePrivatePort];
  62.         [listen addPort];
  63.         speak = [NXApp appSpeaker];
  64.         NXConvertWinNumToGlobal([window windowNum],&windowNum);
  65.         [speak setSendPort:NXPortFromName(NX_WORKSPACEREQUEST,NULL)];
  66.         [speak registerWindow:windowNum toPort:[listen listenPort]];
  67.       //END
  68.        if (!type) {
  69.         sprintf(title,"tarZan V1.0 winNum %d",windowNum);
  70.         [window setTitle:title];
  71.     }
  72.     else {
  73.       [window setTitleAsFilename:fname];
  74.       [matrix setEnabled:NO];
  75.     }
  76.     
  77.         if ([NXApp mainWindow]) {
  78.       [[NXApp mainWindow] getFrame:&myFrame];
  79.       [window moveTo:myFrame.origin.x -10 :myFrame.origin.y +10];
  80.     }
  81.     else
  82.       [window center];
  83.       
  84.   [window makeKeyAndOrderFront:self];  
  85.  
  86.   mode = type;    //This is for the iconReleasedAt: method
  87.   
  88.   switch(type) {
  89.     case 0:  //We are using all available procedures to compress and or tar files!
  90.                 [[matrix cellAt:1 :0] setEnabled:NO];
  91.          [[matrix cellAt:1 :1] setEnabled:NO];
  92.          [[matrix cellAt:1 :2] setEnabled:NO];
  93.          break;
  94.     case 1:  //We are Going to un-tar a file but we must display it's guts etc..
  95.          [[matrix cellAt:1 :0] setEnabled:YES];
  96.          tempFile = NXGetTempFilename(templateName,12);
  97.          if ((fd = creat(tempFile,(int)00666) ) == -1) {
  98.              SHOWERROR("Error in tarZanObject untar","Couldn't create temp file");
  99.          }
  100.          else {
  101.              sprintf(actionMsg,"tar tf %s > %s",fname,tempFile);
  102.         system(actionMsg);
  103.         if (!(tempF = NXMapFile(tempFile,NX_READONLY))) {
  104.             SHOWERROR("Error in tarZan Object","Couldn't open Stream using fd");
  105.         }
  106.         else {
  107.             oldCount = 0;
  108.             while (!NXAtEOS(tempF)) {
  109.               tempChar = NXGetc(tempF);
  110.               if (tempChar != '\n')
  111.                   streamText[tempCount++] = tempChar;
  112.               else if (tempChar == '\n') {
  113.                 streamText[tempCount] = '\0';
  114.                 tempCount = 0;              
  115.                 ++oldCount;
  116.                 [[browser matrixInColumn:0] addRow];
  117.                  [[browser matrixInColumn:0] putCell:[[[[myBrowserCell alloc] initTextCell:streamText] setLeaf:YES] setLoaded:YES] at:oldCount-1 :0];
  118.               }
  119.                }
  120.                [browser reloadColumn:0];
  121.                NXClose(tempF);
  122.         }
  123.          }
  124.          unlink(tempFile);
  125.              break;
  126.     case 3:  //We are going to uncompress and un-tar the file... display guts!
  127.              [[matrix cellAt:1 :2] setEnabled:YES];
  128.          tempFile = NXGetTempFilename(templateName,12);
  129.          if ( (fd = creat(tempFile,(int)00666) ) == -1) {
  130.              SHOWERROR("Error in tarZanObject","Couldn't create temp file");
  131.          }
  132.          else {
  133.              sprintf(actionMsg,"zcat %s | tar tf - > %s",fname,tempFile);
  134.         system(actionMsg);
  135.         if (!(tempF = NXMapFile(tempFile,NX_READONLY))) {
  136.             SHOWERROR("Error in tarZan Object","Couldn't open Stream using fd");
  137.         }
  138.         else {
  139.             oldCount = 0;
  140.             while (!NXAtEOS(tempF)) {
  141.               tempChar = NXGetc(tempF);
  142.               if (tempChar != '\n')
  143.                   streamText[tempCount++] = tempChar;
  144.               else if (tempChar == '\n') {
  145.                 streamText[tempCount] = '\0';
  146.                 tempCount = 0;              
  147.                 ++oldCount;
  148.                 [[browser matrixInColumn:0] addRow];
  149.                  [[browser matrixInColumn:0] putCell:[[[[myBrowserCell alloc] initTextCell:streamText] setLeaf:YES] setLoaded:YES] at:oldCount-1 :0];
  150.               }
  151.                }
  152.                [browser reloadColumn:0];
  153.                NXClose(tempF);
  154.         }
  155.          }
  156.          unlink(tempFile);
  157.          break;
  158.     default: 
  159.              break;
  160.   }
  161.  
  162.   return self;
  163. }
  164.  
  165. - procedure:sender
  166. {    BOOL isThere = NO;
  167.     SavePanel *savePanel;
  168.     const char *tmpChar,*tmp2Char;
  169.     char tarBuffer[1024],tmpBuffer[1024],*tmpPtr;
  170.     int totalCount = 0,decCount = 0,tmpCount = 0,charCount = 0;
  171.     
  172.     switch([[sender selectedCell] tag]) {
  173.         case 0:    //tar a file(s)
  174.             if (!selectionMade)
  175.                 return self;
  176.             if (oldCount == 0)
  177.                 return self;
  178.             savePanel = [SavePanel new];
  179.             [savePanel setTitle:"tar"];
  180.             [savePanel setRequiredFileType:"tar"];
  181.             if ([savePanel runModalForDirectory:NXHomeDirectory() file:"untitled"]) {
  182.                 tmpCount = oldCount;
  183.                 while (![[[browser matrixInColumn:0] cellAt:tmpCount :0] isHighlighted])
  184.                     --tmpCount;
  185.                 tmpChar = [[[browser matrixInColumn:0] cellAt:tmpCount :0] stringValue];
  186.                 tmpPtr = strrchr(tmpChar,'/');
  187.                 for (charCount = 0; charCount <= (tmpPtr-tmpChar) ; charCount++)
  188.                     tmpBuffer[charCount] = tmpChar[charCount];
  189.                 tmpBuffer[(tmpPtr-tmpChar)+1] = '\0';
  190.                 sprintf(tarBuffer,"(cd %s ; tar cf %s %s)",tmpBuffer,[savePanel filename],tmpPtr+1);
  191.                 [[browser matrixInColumn:0] removeRowAt:tmpCount andFree:YES];
  192.                 decCount++;
  193.                 system(tarBuffer);
  194.                 for (totalCount = tmpCount ; totalCount >= 0; --totalCount) {
  195.                     if ([[[browser matrixInColumn:0] cellAt:totalCount :0] isHighlighted]) {
  196.                         tmpChar = [[[browser matrixInColumn:0] cellAt:totalCount :0] stringValue];
  197.                         tmpPtr = strrchr(tmpChar,'/');
  198.                         for (charCount = 0; charCount <= (tmpPtr-tmpChar) ; charCount++)
  199.                             tmpBuffer[charCount] = tmpChar[charCount];
  200.                         tmpBuffer[(tmpPtr-tmpChar)+1] = '\0';
  201.                         sprintf(tarBuffer,"(cd %s ; tar uf %s %s)",tmpBuffer,[savePanel filename],tmpPtr+1);
  202.                         [[browser matrixInColumn:0] removeRowAt:totalCount andFree:YES];
  203.                         decCount = decCount + 1;
  204.                         isThere = YES;
  205.                         system(tarBuffer);
  206.                     }
  207.                 }
  208.                 oldCount = (oldCount - decCount);
  209.                 if (oldCount <= 0 ) selectionMade = NO;
  210.                 [browser reloadColumn:0];
  211.             }
  212.             break;
  213.         case 1:    //compress a file(s)
  214.             if (!selectionMade)
  215.                 return self;
  216.             if (oldCount == 0)
  217.                 return self;
  218.             if (!NXRunAlertPanel("Hold On!","Are You Sure You Want to do This? ","YES","NO",NULL))
  219.                 return self;
  220.             for (totalCount = oldCount; totalCount >= 0; --totalCount) {
  221.                 if ([[[browser matrixInColumn:0] cellAt:totalCount :0] isHighlighted]) {
  222.                     sprintf(tarBuffer,"compress %s",[[[browser matrixInColumn:0] cellAt:totalCount :0] stringValue]);
  223.                     system(tarBuffer);
  224.                     [[browser matrixInColumn:0] removeRowAt:totalCount andFree:YES];
  225.                     decCount = decCount + 1;
  226.                 }
  227.             }
  228.             oldCount = oldCount - decCount;
  229.             if (oldCount <= 0 ) selectionMade = NO;
  230.             [browser reloadColumn:0];
  231.             break;
  232.         case 2: //tar & compress file(s)
  233.             if (!selectionMade)
  234.                 return self;
  235.             if (oldCount == 0)
  236.                 return self;
  237.             savePanel = [SavePanel new];
  238.             [savePanel setTitle:"tar & compress"];
  239.             [savePanel setRequiredFileType:"tar"];
  240.             if ([savePanel runModalForDirectory:NXHomeDirectory() file:"untitled"]) {
  241.                 tmpCount = oldCount;
  242.                 while (![[[browser matrixInColumn:0] cellAt:tmpCount :0] isHighlighted])
  243.                     --tmpCount;
  244.                 tmpChar = [[[browser matrixInColumn:0] cellAt:tmpCount :0] stringValue];
  245.                 tmpPtr = strrchr(tmpChar,'/');
  246.                 for (charCount = 0; charCount <= (tmpPtr-tmpChar) ; charCount++)
  247.                     tmpBuffer[charCount] = tmpChar[charCount];
  248.                 tmpBuffer[(tmpPtr-tmpChar)+1] = '\0';
  249.                 sprintf(tarBuffer,"(cd %s ; tar cf %s %s)",tmpBuffer,[savePanel filename],tmpPtr+1);
  250.                 [[browser matrixInColumn:0] removeRowAt:tmpCount andFree:YES];
  251.                 decCount++;
  252.                 system(tarBuffer);
  253.                 for (totalCount = tmpCount ; totalCount >= 0; --totalCount) {
  254.                     if ([[[browser matrixInColumn:0] cellAt:totalCount :0] isHighlighted]) {
  255.                         tmpChar = [[[browser matrixInColumn:0] cellAt:totalCount :0] stringValue];
  256.                         tmpPtr = strrchr(tmpChar,'/');
  257.                         for (charCount = 0; charCount <= (tmpPtr-tmpChar) ; charCount++)
  258.                             tmpBuffer[charCount] = tmpChar[charCount];
  259.                         tmpBuffer[(tmpPtr-tmpChar)+1] = '\0';
  260.                         sprintf(tarBuffer,"(cd %s ; tar uf %s %s)",tmpBuffer,[savePanel filename],tmpPtr+1);
  261.                         [[browser matrixInColumn:0] removeRowAt:totalCount andFree:YES];
  262.                         decCount = decCount + 1;
  263.                         isThere = YES;
  264.                         system(tarBuffer);
  265.                     }
  266.                 }
  267.                 oldCount = (oldCount - decCount);
  268.                 if (oldCount <= 0) selectionMade = NO;
  269.                 [browser reloadColumn:0];
  270.                 sprintf(tarBuffer,"compress %s",[savePanel filename]);
  271.                 system(tarBuffer);
  272.                 
  273.             }
  274.             break;
  275.         case 3: //untar file(s)
  276.             if (!selectionMade) 
  277.                 return self;
  278.             if (oldCount <= 0)
  279.                 return self;
  280.             savePanel = [SavePanel new];
  281.             [savePanel setTitle:"untar"];
  282.             if([savePanel runModalForDirectory:NXHomeDirectory() file:"Do_Not_Erase_This"]) {
  283.                 tmpChar = [savePanel directory];
  284.                 for (totalCount = oldCount ; totalCount >=0 ; --totalCount) {
  285.                     if ([[[browser matrixInColumn:0] cellAt:totalCount :0] isHighlighted]) {
  286.                             tmp2Char = [[[browser matrixInColumn:0] cellAt:totalCount :0] stringValue];
  287.                             strcpy(tarBuffer,"(cd / ; tar xf ");
  288.                             strcpy(tarBuffer,"(cd ");
  289.                             strcat(tarBuffer,tmpChar);
  290.                             strcat(tarBuffer," ; tar xf ");
  291.                             
  292.                             strcat(tarBuffer,globalName);
  293.                             strcat(tarBuffer," ");
  294.                             strcat(tarBuffer,tmp2Char);
  295.                             strcat(tarBuffer,")");
  296.                             [[browser matrixInColumn:0] removeRowAt:totalCount andFree:YES];
  297.                             decCount = decCount + 1;
  298.                             system(tarBuffer);
  299.                     }
  300.                 }
  301.                 oldCount = (oldCount - decCount);
  302.                 [browser reloadColumn:0];
  303.             }
  304.             if (oldCount <= 0)
  305.                 [window close];
  306.             break;
  307.         case 5: //untar & uncompress file(s)
  308.             if (!selectionMade) 
  309.                 return self;
  310.             if (oldCount <= 0)
  311.                 return self;
  312.             savePanel = [SavePanel new];
  313.             [savePanel setTitle:"untar"];
  314.             if([savePanel runModalForDirectory:NXHomeDirectory() file:"Do_Not_Erase_This"]) {
  315.                 tmpChar = [savePanel directory];
  316.                 sprintf(tarBuffer,"(cd %s ; zcat %s | tar xf - ",tmpChar,globalName);
  317.                 for (totalCount = oldCount ; totalCount >=0 ; --totalCount) {
  318.                     if ([[[browser matrixInColumn:0] cellAt:totalCount :0] isHighlighted]) {
  319.                             strcat(tarBuffer,[[[browser matrixInColumn:0] cellAt:totalCount :0] stringValue]);
  320.                             strcat(tarBuffer," ");
  321.                             [[browser matrixInColumn:0] removeRowAt:totalCount andFree:YES];
  322.                             decCount = decCount + 1;
  323.                             isThere = YES;
  324.                     }
  325.                 }
  326.                 strcat(tarBuffer,")");
  327.                 oldCount = (oldCount - decCount);
  328.                 [browser reloadColumn:0];
  329.                 if (isThere)
  330.                     system(tarBuffer);
  331.                 if (oldCount <=0)
  332.                     [window close];
  333.             }
  334.             break;
  335.         default:
  336.             break;
  337.     }
  338.     return self;
  339. }
  340.  
  341. - free
  342. {    unsigned int windowNum;
  343.     
  344.     NXConvertWinNumToGlobal([window windowNum],&windowNum);
  345.     [speak unregisterWindow:windowNum];
  346.     [browser free];
  347.     [window free];
  348.     return [super free];
  349. }
  350.  
  351. // Window Delegate Methods
  352. - windowWillClose:sender
  353. {
  354.     [NXApp delayedFree:self];
  355.     return self;
  356. }
  357.  
  358. // Browser  Delegate Methods
  359. - (int)browser:sender fillMatrix:tmatrix inColumn:(int)column
  360. {
  361.     int i;
  362.  
  363.     for (i = 0; i<oldCount;i++) 
  364.         [sender getLoadedCellAtRow:i inColumn:0];
  365.     return oldCount;
  366. }
  367.  
  368. // Listener Delegate Methods
  369. - (int)iconEntered:(int)windowNum at:(double)x :(double)y 
  370.     iconWindow:(int)iconWindowNum iconX:(double)iconX iconY:(double)iconY 
  371.     iconWidth:(double)iconWidth iconHeight:(double)iconHeight
  372.     pathList:(const char *)pathList
  373. {
  374.   if (!iconPathList) goto icLabel1;
  375.   if (strcmp(iconPathList,pathList)) goto icLabel1;
  376.   goto icLabel2;
  377. icLabel1:
  378.   NX_FREE(iconPathList);
  379.   iconPathList = NXCopyStringBuffer(pathList);
  380. icLabel2:  
  381.   return 0;
  382. }
  383.  
  384.      char *ir_tmpChar,ir_action[1024],dirBuff[255],another[1024],*file,*ptr;
  385.     int icount;
  386.     
  387. - (int)iconReleasedAt:(double)x :(double)y ok:(int *)flag
  388. {
  389.     
  390.   file = iconPathList;
  391.  NX_DURING
  392.    file = iconPathList; 
  393.  NX_HANDLER
  394.  NX_ENDHANDLER
  395.  if (!file) 
  396.    *flag = 0;
  397.  else
  398.    *flag = 1;
  399.  
  400.     
  401.     if (mode == 3) {
  402.          sprintf(ir_action,"uncompress %s",globalName);
  403.         system(ir_action);
  404.         strcpy(another,globalName);    //Once we uncompress the string we must reflect the .tar so 
  405.         another[strlen(another)-2] = '\0';// we get rid of the .Z part of the string by placing a NULL where the . is
  406.     }
  407.  
  408.      ptr = strtok(file,"\t");
  409.      ++oldCount;
  410.      [[browser matrixInColumn:0] addRow];
  411.     if ( (mode == 1) || (mode == 3) ) {
  412.         ir_tmpChar = strrchr(ptr,'/');
  413.         for (icount = 0; icount <= (ir_tmpChar-ptr) ; icount++)
  414.             dirBuff[icount] = ptr[icount];
  415.         dirBuff[(ir_tmpChar-ptr)+1] = '\0';
  416.         if (mode == 3)
  417.             sprintf(ir_action,"(cd %s ; tar uf %s %s)",dirBuff,another,ir_tmpChar+1);
  418.         if (mode == 1)
  419.             sprintf(ir_action,"(cd %s ; tar uf %s %s)",dirBuff,globalName,ir_tmpChar+1);
  420.         system(ir_action);
  421.     }
  422.     if ((mode == 1) || (mode == 3))
  423.          [[browser matrixInColumn:0] putCell:[[[[myBrowserCell alloc] initTextCell:ir_tmpChar+1] setLeaf:YES] setLoaded:YES] at:oldCount-1 :0];
  424.     else
  425.         [[browser matrixInColumn:0] putCell:[[[[myBrowserCell alloc] initTextCell:ptr] setLeaf:YES] setLoaded:YES] at:oldCount-1 :0];
  426.      while((ptr = strtok(NULL,"\t"))!= NULL) {
  427.        ++oldCount;
  428.        [[browser matrixInColumn:0] addRow];
  429.     if ( (mode == 1) || (mode == 3) ) {
  430.         ir_tmpChar = strrchr(ptr,'/');
  431.         for (icount = 0; icount <= (ir_tmpChar-ptr) ; icount++)
  432.             dirBuff[icount] = ptr[icount];
  433.         dirBuff[(ir_tmpChar-ptr)+1] = '\0';
  434.         if (mode == 3)
  435.             sprintf(ir_action,"(cd %s ; tar uf %s %s)",dirBuff,another,ir_tmpChar+1);
  436.         if (mode == 1)
  437.             sprintf(ir_action,"(cd %s ; tar uf %s %s)",dirBuff,globalName,ir_tmpChar+1);
  438.         system(ir_action);
  439.     }
  440.  
  441.        if ((mode == 1) || (mode == 3))
  442.          [[browser matrixInColumn:0] putCell:[[[[myBrowserCell alloc] initTextCell:ir_tmpChar+1] setLeaf:YES] setLoaded:YES] at:oldCount-1 :0];
  443.     else
  444.         [[browser matrixInColumn:0] putCell:[[[[myBrowserCell alloc] initTextCell:ptr] setLeaf:YES] setLoaded:YES] at:oldCount-1 :0];
  445.  
  446.      }
  447.      if (mode == 3) {
  448.         sprintf(ir_action,"compress %s",another);
  449.         system(ir_action);
  450.     }
  451.      [browser reloadColumn:0];
  452.  return 0;
  453. }
  454. @end